home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / ISp Sample / Source / MainWindow.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  13.8 KB  |  608 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MainWindow.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                xxx put dri here xxx
  13.  
  14.         Other Contact:        xxx put other contact here xxx
  15.  
  16.         Technology:            xxx put technology here xxx
  17.  
  18.     Writers:
  19.  
  20.         (BWS)    Brent Schorsch
  21.  
  22.     Change History (most recent first):
  23.  
  24.        <SP1>      7/1/99    BWS        first checked in
  25. */
  26.  
  27. //•    ——————————————————————————————    Includes
  28.  
  29. #include <Controls.h>
  30. #include <FixMath.h>
  31. #include <Fonts.h>
  32. #include <MacWindows.h>
  33. #include <NumberFormatting.h>
  34. #include <QuickDraw.h>
  35. #include <ToolUtils.h>
  36. #include <TextUtils.h>
  37.  
  38. #include <stdio.h>
  39.  
  40. #include "AppShellResources.h"
  41. #include "ISp_SampleResources.h"
  42. #include "MainWindow.h"
  43. #include "ErrorAlert.h"
  44. #include "ISp_Sample.h"
  45.  
  46. //•    ——————————————————————————————    Private Definitions
  47.  
  48. // drawing
  49. #define kButtonsLineStart            40
  50. #define kAxisLineStart                100
  51.  
  52. #define    kLineHeight                    12
  53.  
  54. #define    kLabelsIndent                60
  55. #define    kColumnIndent                150
  56.  
  57. // physics
  58. #define        kMin_Angle            (-180.0)
  59. #define        kMax_Angle            ( 180.0)
  60.  
  61. #define        kThrottleCoef        (   0.01)
  62. #define        kFrictionCoef        (   0.10)
  63. #define        k2ndFrictionCoef    (   0.01)
  64.  
  65.  
  66. //•    ——————————————————————————————    Private Types
  67. //•    ——————————————————————————————    Private Variables
  68. //•    ——————————————————————————————    Private Functions
  69. //•    ——————————————————————————————    Public Variables
  70.  
  71. MainWindow::MainWindow()
  72. {
  73. OSStatus error;
  74.  
  75.     window = GetNewCWindow(kWIND_MainWindow, nil, (WindowPtr) -1L);
  76.             
  77.     ::SetWindowKind(window, userKind);
  78.     ::SetWRefCon(window, (SInt32) this);
  79.  
  80.     CreateRootControl(window, &rootControl);
  81.  
  82.     SetTitle("\pInputSprocket Sample");
  83.     MakeCurrentPort();
  84.  
  85.     Show();
  86.     
  87.     if (!Input_Available())
  88.         ErrorAlert("\pInputSprocket is required."
  89.                     "Place InputSprocketLib, InputSprocketKeyboard, InputSprocketMouse, etc"
  90.                     " in your active SysyemFolder.", 1, true);
  91.     
  92.     error = Input_Initialize();
  93.     if (error)
  94.         ErrorAlert("\pInputSprocket failed to initialize.", error, true);
  95.  
  96.     Input_InitializeState(&mInputState);
  97. }
  98.  
  99. MainWindow::~MainWindow()
  100. {
  101. }
  102.  
  103. //•    ————————————————————    MainWindow::Update
  104.  
  105. void
  106. MainWindow::Update(void)
  107. {
  108. GrafPtr        oldPort;
  109. WindowPtr    theWindow = window;
  110. Rect        r;
  111.  
  112.     ::GetPort(&oldPort);
  113.     MakeCurrentPort();
  114.  
  115.     ::BeginUpdate(theWindow);
  116.  
  117.     ::ClipRect(&theWindow->portRect);
  118.     ::EraseRect(&theWindow->portRect);
  119.     
  120.     DrawContents();
  121.     
  122.     r = window->portRect;
  123.     r.left = r.right - 15;
  124.     r.top = r.bottom - 15;
  125.     ::ClipRect(&r);
  126.     ::DrawGrowIcon(theWindow);
  127.  
  128.     ::ClipRect(&theWindow->portRect);
  129.  
  130.     ::EndUpdate(theWindow);
  131.     
  132.     ::SetPort(oldPort);
  133. }
  134.  
  135. //•    ————————————————————    MainWindow::Grow
  136.  
  137. OSErr
  138. MainWindow::Grow(const Point inWhere)
  139. {
  140. Rect        sizeRect;
  141. long        result;
  142.  
  143.     sizeRect.top = 300;
  144.     sizeRect.left = 450;
  145.     sizeRect.bottom = 440;
  146.     sizeRect.right = 640;
  147.     
  148.     result = ::GrowWindow(window, inWhere, &sizeRect);
  149.  
  150.     if (0 != result)
  151.     {
  152.     GrafPtr    oldPort;
  153.     
  154.         ::SizeWindow(window, LoWord(result), HiWord(result), false);
  155.         
  156.         ::GetPort(&oldPort);
  157.         MakeCurrentPort();
  158.         
  159.         ::ClipRect(&window->portRect);
  160.         ::InvalRect(&window->portRect);
  161.         
  162.         ::SetPort(oldPort);
  163.     }
  164.  
  165.     AdjustControlPositions();
  166.  
  167.     return (noErr);
  168. }
  169.  
  170. //•    ————————————————————    MainWindow::Click
  171.  
  172. OSErr
  173. MainWindow::Click(const Point inWhere, const short inModifiers)
  174. {
  175. #pragma unused (inModifiers)
  176.  
  177. Point            localPoint;
  178. ControlHandle    theControl;
  179. short            thePart;
  180.  
  181.     if (FrontWindow() != window)
  182.         ::SelectWindow(window);
  183.  
  184.     localPoint = inWhere;
  185.     ::GlobalToLocal(&localPoint);
  186.  
  187.     theControl = ::FindControlUnderMouse(localPoint, window, &thePart);
  188.  
  189.     if (nil != theControl)
  190.     {
  191.         (void) ::TrackControl(theControl, localPoint, nil);
  192.     }
  193.  
  194.     return (noErr);
  195. }
  196.  
  197. //•    ————————————————————    MainWindow::Close
  198.  
  199. OSErr
  200. MainWindow::Close(void)
  201. {
  202.     ::KillControls(window);
  203.     ::DisposeWindow(window);
  204.  
  205.     return (noErr);
  206. }
  207.  
  208. //•    ————————————————————    MainWindow::Idle
  209.  
  210. void
  211. MainWindow::Idle(void)
  212. {
  213. Boolean    gameWasInProgress;
  214. Boolean    gameWasPaused;
  215.  
  216.     ShellWindow::Idle();
  217.     
  218.     gameWasInProgress = mInputState.gameInProgress;
  219.     gameWasPaused = mInputState.gamePaused;
  220.     
  221.     ::Input_GetButtonEvents (&mInputState);
  222.     ::Input_PollAxisValues (&mInputState);
  223.     
  224.     if (mInputState.gameInProgress && !gameWasInProgress)
  225.         StartGame ();
  226.     else if (!mInputState.gameInProgress && gameWasInProgress)
  227.         EndGame ();
  228.     else if (mInputState.gamePaused != gameWasPaused)
  229.         TogglePauseGame ();
  230.     
  231.     // do some physics calculations
  232.     mAngleOfRoll = AdjustAngleByAxisInput (mAngleOfRoll, mInputState.rollInput);
  233.     mAngleOfRoll = AdjustAngleByFixedDelta (mAngleOfRoll, mInputState.deltaRoll);
  234.     
  235.     mAngleOfPitch = AdjustAngleByAxisInput (mAngleOfPitch, mInputState.pitchInput);;
  236.     mAngleOfPitch = AdjustAngleByFixedDelta (mAngleOfPitch, mInputState.deltaPitch);;
  237.  
  238.     mAngleOfYaw = AdjustAngleByAxisInput (mAngleOfYaw, mInputState.yawInput);
  239.     mAngleOfYaw = AdjustAngleByFixedDelta (mAngleOfYaw, mInputState.deltaYaw);
  240.  
  241.     mVelocity = mVelocity + (kThrottleCoef * mInputState.throttleInput) - kFrictionCoef - (k2ndFrictionCoef * mVelocity);
  242.     if (mVelocity < 0) mVelocity = 0;
  243.     
  244.     DrawValues();
  245. }
  246.  
  247. //•    ————————————————————    MainWindow::Menu
  248.  
  249. Boolean
  250. MainWindow::Menu(const UInt32 inMenuCommand, const short inModifiers)
  251. {
  252. #pragma unused (inModifiers)
  253.  
  254. Boolean        handled = true;
  255.  
  256.     switch (inMenuCommand)
  257.     {
  258.         case kMenuCMD_StartGame:
  259.             StartGame ();
  260.             break;
  261.             
  262.         case kMenuCMD_ConfigureInput:
  263.             Input_ShowConfigureDialog();
  264.             break;
  265.         
  266.         default:
  267.             handled = false;
  268.             break;
  269.     }
  270.  
  271.  
  272.     return (handled);
  273. }
  274.  
  275. //•    ————————————————————    MainWindow::Suspend
  276.  
  277. void
  278. MainWindow::Suspend(void)
  279. {
  280.     Input_Suspend ();
  281. }
  282.  
  283. //•    ————————————————————    MainWindow::Resume
  284.  
  285. void
  286. MainWindow::Resume(void)
  287. {
  288.     Input_Resume ();
  289. }
  290.  
  291. //•    ————————————————————    MainWindow::DrawContents
  292.  
  293. void
  294. MainWindow::DrawContents(void)
  295. {
  296. GrafPtr    oldPort;
  297. Rect    r;
  298.  
  299.     ::GetPort(&oldPort);
  300.     MakeCurrentPort();
  301.     
  302.     ::DrawControls(window);
  303.     
  304.     ::TextFace(bold);
  305.     ::TextFont(kFontIDGeneva);
  306.     ::TextSize(9);
  307.     
  308.     //• erase the game in progress/paused area
  309.     r.left = 0;
  310.     r.right = Width();
  311.     r.top = 0;
  312.     r.bottom = 15;
  313.     EraseRect (&r);
  314.     
  315.     //• draw the game in progress/paused area
  316.     if (!mInputState.gameInProgress)
  317.         DrawStringCentered("\pPress Start or select New Game to start", kLineHeight);
  318.     else
  319.     {
  320.         if (mInputState.gamePaused)
  321.             DrawStringCentered("\pPaused", kLineHeight);
  322.         else
  323.             DrawStringCentered("\pGame in progress, MacOS cursor and menus disabled", kLineHeight);
  324.     }
  325.     
  326.     //• draw button captions
  327.                                                                         // skip line for 'Firing Weapon' caption
  328.     DrawStringRightJustified ("\pShots Fired Weapon:",     kLabelsIndent * 2, kButtonsLineStart + (kLineHeight * 1), 0);
  329.     DrawStringRightJustified ("\pCurrent Weapon:",         kLabelsIndent * 2, kButtonsLineStart + (kLineHeight * 2), 0);
  330.  
  331.     //• draw axis captions
  332.     DrawStringRightJustified ("\pRoll:",         kLabelsIndent, kAxisLineStart + (kLineHeight * 0), 0);
  333.     DrawStringRightJustified ("\pPitch:",         kLabelsIndent, kAxisLineStart + (kLineHeight * 1), 0);
  334.     DrawStringRightJustified ("\pYaw:",         kLabelsIndent, kAxisLineStart + (kLineHeight * 2), 0);
  335.     DrawStringRightJustified ("\pThrottle:",     kLabelsIndent, kAxisLineStart + (kLineHeight * 3), 0);
  336.     
  337.     DrawStringRightJustified ("\pRoll Delta:",     kColumnIndent + kLabelsIndent, kAxisLineStart + (kLineHeight * 0), 0);
  338.     DrawStringRightJustified ("\pPitch Delta:", kColumnIndent + kLabelsIndent, kAxisLineStart + (kLineHeight * 1), 0);
  339.     DrawStringRightJustified ("\pYaw Delta:",     kColumnIndent + kLabelsIndent, kAxisLineStart + (kLineHeight * 2), 0);
  340.  
  341.     DrawStringRightJustified ("\pAngle of Roll:",     (kColumnIndent * 2) + kLabelsIndent, kAxisLineStart + (kLineHeight * 0), 0);
  342.     DrawStringRightJustified ("\pAngle of Pitch:",     (kColumnIndent * 2) + kLabelsIndent, kAxisLineStart + (kLineHeight * 1), 0);
  343.     DrawStringRightJustified ("\pAngle of Yaw:",     (kColumnIndent * 2) + kLabelsIndent, kAxisLineStart + (kLineHeight * 2), 0);
  344.     DrawStringRightJustified ("\pVelocity:",         (kColumnIndent * 2) + kLabelsIndent, kAxisLineStart + (kLineHeight * 3), 0);
  345.     
  346.  
  347.     DrawValues();
  348.  
  349.     ::SetPort(oldPort);
  350. }
  351.  
  352. //•    ————————————————————    MainWindow::DrawValues
  353.  
  354. void
  355. MainWindow::DrawValues(void)
  356. {
  357. GrafPtr    oldPort;
  358. Str255    string;
  359.  
  360.     ::GetPort(&oldPort);
  361.     MakeCurrentPort();
  362.     
  363.     ::TextMode(srcCopy);
  364.     
  365.     if (mInputState.gameInProgress)
  366.     {
  367.         // Firing weapon
  368.         SInt16    numbersIndent = (kLabelsIndent * 2) + 3;
  369.         
  370.         if (mInputState.fireWeaponState)
  371.             DrawStringRightJustified ("\pFiring _Now_", kLabelsIndent * 2, kButtonsLineStart + (kLineHeight * 0), 100);
  372.         else
  373.             DrawStringRightJustified ("\p", kLabelsIndent * 2, kButtonsLineStart + (kLineHeight * 0), 100);
  374.         
  375.         ::NumToString (mInputState.fireWeaponCount, string);
  376.         DrawStringLeftJustified (string,                 numbersIndent, kButtonsLineStart + (kLineHeight * 1), 20);
  377.         
  378.         GetWeaponString (mInputState.currentWeapon, string);
  379.         DrawStringLeftJustified (string,                 numbersIndent, kButtonsLineStart + (kLineHeight * 2), 150);
  380.  
  381.         // axis values
  382.         numbersIndent -= kLabelsIndent;
  383.         
  384.         ::NumToString (mInputState.rollInput, string);
  385.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 0), 30);
  386.         
  387.         ::NumToString (mInputState.pitchInput, string);
  388.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 1), 30);
  389.  
  390.         ::NumToString (mInputState.yawInput, string);
  391.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 2), 30);
  392.  
  393.         ::NumToString (mInputState.throttleInput, string);
  394.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 3), 30);
  395.         
  396.         // delta values
  397.         numbersIndent += kColumnIndent;
  398.         
  399.         FixedToString (mInputState.deltaRoll, string);
  400.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 0), 30);
  401.         
  402.         FixedToString (mInputState.deltaPitch, string);
  403.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 1), 30);
  404.  
  405.         FixedToString (mInputState.deltaYaw, string);
  406.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 2), 30);
  407.  
  408.         // physics values
  409.         numbersIndent += kColumnIndent;
  410.  
  411.         FloatToString (mAngleOfRoll, string);
  412.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 0), 30);
  413.         
  414.         FloatToString (mAngleOfPitch, string);
  415.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 1), 30);
  416.  
  417.         FloatToString (mAngleOfYaw, string);
  418.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 2), 30);
  419.  
  420.         FloatToString (mVelocity, string);
  421.         DrawStringLeftJustified (string, numbersIndent, kAxisLineStart + (kLineHeight * 3), 30);
  422.     }
  423.     
  424.     ::TextMode(srcOr);
  425.  
  426.     ::SetPort(oldPort);
  427. }
  428.  
  429. //•    ————————————————————    MainWindow::AdjustControlPositions
  430.  
  431. void
  432. MainWindow::AdjustControlPositions(void)
  433. {
  434. }
  435.  
  436. //•    ————————————————————    MainWindow::StartGame
  437.  
  438. void
  439. MainWindow::StartGame(void)
  440. {
  441.     Input_InitializeState(&mInputState);
  442.  
  443.     mInputState.gameInProgress = true;
  444.     mInputState.gamePaused = false;
  445.     
  446.     Input_DisableKeyboardForTyping();
  447.     Input_DisableMouseForCursor();
  448.     
  449.     ::HideCursor();
  450.     
  451.     mAngleOfRoll = 0.0;
  452.     mAngleOfPitch = 0.0;
  453.     mAngleOfYaw = 0.0;
  454.     mVelocity = 0.0;
  455.  
  456.     DrawContents();
  457. }
  458.  
  459. //•    ————————————————————    MainWindow::EndGame
  460.  
  461. void
  462. MainWindow::EndGame(void)
  463. {
  464.     mInputState.gameInProgress = false;
  465.     mInputState.gamePaused = false;
  466.     
  467.     Input_EnableKeyboardForTyping();
  468.     Input_EnableMouseForCursor();
  469.  
  470.     ::InitCursor();
  471.  
  472.     DrawContents();
  473. }
  474.  
  475. //•    ————————————————————    MainWindow::TogglePauseGame
  476.  
  477. void
  478. MainWindow::TogglePauseGame(void)
  479. {
  480.     if (mInputState.gamePaused)
  481.     {
  482.         Input_EnableKeyboardForTyping();
  483.         Input_EnableMouseForCursor();
  484.  
  485.         ::ShowCursor();
  486.     }
  487.     else
  488.     {
  489.         Input_DisableKeyboardForTyping();
  490.         Input_DisableMouseForCursor();
  491.  
  492.         ::HideCursor();
  493.     }
  494.     
  495.     DrawContents();
  496. }
  497.  
  498. //•    ————————————————————    MainWindow::GetWeaponString
  499. void
  500. MainWindow::GetWeaponString (SInt16 inWeapon, StringPtr ioString)
  501. {
  502.     GetIndString (ioString, kSTRn_WeaponNames, inWeapon + 1);
  503. }
  504.  
  505. //•    ————————————————————    MainWindow::DrawStringCentered
  506. void
  507. MainWindow::DrawStringCentered (StringPtr string, SInt16 height)
  508. {
  509. UInt32    width;
  510.  
  511.     width = StringWidth(string);
  512.     ::MoveTo((Width() / 2) - (width / 2), height);
  513.     ::DrawString(string);
  514. }
  515.  
  516. //•    ————————————————————    MainWindow::DrawStringLeftJustified
  517. void
  518. MainWindow::DrawStringLeftJustified (StringPtr string, SInt16 indent, SInt16 height, SInt16 eraseSize)
  519. {
  520. UInt32    width;
  521. Rect    r;
  522.  
  523.     width = StringWidth(string);
  524.     
  525.     if (eraseSize)
  526.     {
  527.         r.left = indent + width;
  528.         r.right = r.left + eraseSize;
  529.         r.top = height - 10;
  530.         r.bottom = height + 2;
  531.         EraseRect (&r);
  532.     }
  533.     
  534.     ::MoveTo(indent, height);
  535.     ::DrawString(string);
  536. }
  537.  
  538. //•    ————————————————————    MainWindow::DrawStringRightJustified
  539. void
  540. MainWindow::DrawStringRightJustified (StringPtr string, SInt16 indent, SInt16 height, SInt16 eraseSize)
  541. {
  542. UInt32    width;
  543. Rect    r;
  544.  
  545.     width = StringWidth(string);
  546.  
  547.     if (eraseSize)
  548.     {
  549.         r.right = indent - width;
  550.         r.left = r.right - eraseSize;
  551.         r.top = height - 10;
  552.         r.bottom = height + 2;
  553.         EraseRect (&r);
  554.     }
  555.     
  556.     ::MoveTo(indent - width, height);
  557.     ::DrawString(string);
  558. }
  559.  
  560. //•    ————————————————————    MainWindow::FixedToString
  561. void
  562. MainWindow::FixedToString (Fixed inFixed, StringPtr ioString)
  563. {
  564. float    inches = Fix2X (inFixed);
  565.  
  566.     sprintf((char *) ioString, "%01.02f", inches * 100.0);
  567.     c2pstr((char *) ioString);
  568. }
  569.  
  570. //•    ————————————————————    MainWindow::FloatToString
  571. void
  572. MainWindow::FloatToString (float inFloat, StringPtr ioString)
  573. {
  574.     sprintf((char *) ioString, "%01.01f", inFloat);
  575.     c2pstr((char *) ioString);
  576. }
  577.  
  578. //•    ————————————————————    MainWindow::AdjustAngleByAxisInput
  579. float
  580. MainWindow::AdjustAngleByAxisInput (float inCurrentAngle, SInt32 inAxisValue)
  581. {
  582.     float    newAngle = inCurrentAngle + (inAxisValue / (kMax_Roll * 0.5));
  583.     
  584.     while (newAngle < kMin_Angle)
  585.         newAngle += (kMax_Angle - kMin_Angle);
  586.  
  587.     while (newAngle > kMax_Angle)
  588.         newAngle -= (kMax_Angle - kMin_Angle);
  589.     
  590.     return newAngle;
  591. }
  592.  
  593. //•    ————————————————————    MainWindow::AdjustAngleByFixedDelta
  594. float
  595. MainWindow::AdjustAngleByFixedDelta (float inCurrentAngle, Fixed inDeltaValue)
  596. {
  597.     float    newAngle = inCurrentAngle + (Fix2X(inDeltaValue) * 50.0);
  598.     
  599.     while (newAngle < kMin_Angle)
  600.         newAngle += (kMax_Angle - kMin_Angle);
  601.  
  602.     while (newAngle > kMax_Angle)
  603.         newAngle -= (kMax_Angle - kMin_Angle);
  604.     
  605.     return newAngle;
  606. }
  607.  
  608.